#include <bits/stdc++.h>

#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define in read<int>()
#define lin read<ll>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;
using ull = unsigned long long;

template < typename T > T read() {
	T x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

const int M = 18;
const int V = 1 << M;
const int N = 3e7 + 10;

int mod;

struct mat {
	ll arr[2][2];
	mat() { arr[0][0] = arr[0][1] = arr[1][0] = arr[1][1] = 0; }
	ll* operator [](int x) { return arr[x]; }
	friend mat operator * (mat a, mat b) {
		mat c; rep(i, 0, 1) rep(j, 0, 1) if(a[i][j]) rep(k, 0, 1) (c[i][k] += a[i][j] * b[j][k] % mod) %= mod;
		return c;
	}
} pre[2][V + 10], P;

char s[N];

mt19937_64 rnd(19491001);

unordered_map < ull, ll > f;

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	scanf("%s", s + 1); mod = in;
	pre[0][0][0][0] = pre[0][0][1][1] = 1; pre[1][0] = pre[0][0]; pre[0][1][0][1] = pre[0][1][1][0] = pre[0][1][1][1] = 1;
	rep(i, 2, V) pre[0][i] = pre[0][i - 1] * pre[0][1];
	pre[1][1] = pre[0][V]; rep(i, 2, V) pre[1][i] = pre[1][i - 1] * pre[1][1];
	ll len = 0;
	while(true) {
		ll x = rnd() << 28 >> 28; mat res = pre[0][x & (V - 1)] * pre[1][x >> M];
		ll f1 = res[0][0], f0 = res[0][1]; ull tres = (ull(f0) << 32) + f1;
		if(f.count(tres)) { len = abs(x - f[tres]); break; }
		f[tres] = x;
	} ll res = 0; int ls = strlen(s + 1); rep(i, 1, ls) res = res * 10 % len + s[i] - '0';
	printf("%lld\n", (pre[0][res & (V - 1)] * pre[1][res / V])[0][1]);
	return 0;
}
